package parser

import (
	"org/leazen/golearning/crawler/engine"
	"regexp"
	"strconv"
	"org/leazen/golearning/crawler/model"
	"log"
)

var ageRe = regexp.MustCompile(`<td><span class="label">年龄：</span>([\d]+)岁</td>`)

var heightRe = regexp.MustCompile(`<td><span class="label">身高：</span>([\d]+)CM</td>`)

var inComeRe  = regexp.MustCompile(`<td><span class="label">月收入：</span>([^<]+)</td>`)

var marriageRe  = regexp.MustCompile(`<td><span class="label">婚况：</span>([^<]+)</td>`)

var educationRe  = regexp.MustCompile(`<td><span class="label">学历：</span>([^<]+)</td>`)

var houseRe  = regexp.MustCompile(`<td><span class="label">住房条件：</span><span field="">([^<]+)</span></td>`)

var carRe  = regexp.MustCompile(`<td><span class="label">是否购车：</span><span field="">([^<]+)</span></td>`)

func ParseProfile(contents []byte)engine.ParseResult  {
	profile := model.Profile{}


	ageMatch := ageRe.FindSubmatch(contents)
	if ageMatch!=nil {
		age, err := strconv.Atoi(string(ageMatch[1]))
		if err == nil{
				profile.Age = age
		}
	}



	heightRe := heightRe.FindSubmatch(contents)
	if heightRe!=nil {
		height, err := strconv.Atoi(string(heightRe[1]))
		if err == nil{
			profile.Height = height
		}
	}

	inComeMatch := inComeRe.FindSubmatch(contents)
	if inComeMatch!=nil {
			profile.Income = string(inComeMatch[1])
	}



	marriageMatch := marriageRe.FindSubmatch(contents)
	if marriageMatch!=nil {
		profile.Marriage = string(marriageMatch[1])
	}

	educationMatch := educationRe.FindSubmatch(contents)
	if educationMatch!=nil {
		profile.Education = string(educationMatch[1])
	}


	houseReMatch := houseRe.FindSubmatch(contents)
	if houseReMatch!=nil {
		profile.House = string(houseReMatch[1])
	}


	carReMatch := carRe.FindSubmatch(contents)
	if carReMatch!=nil {
		profile.Car = string(carReMatch[1])
	}


	log.Printf("user  %d ， %d， %s ,%s, %s, %s, %s," ,profile.Age,profile.Weight,profile.Income,profile.Marriage,profile.Education,profile.House,profile.Car)

	return engine.ParseResult{
		Items:[]interface{}{profile},
	}
}
